package com.dao.impl;

import com.dao.UserDAO;
import com.entity.User;
import com.util.DbUtil;

import java.sql.*;

public class UserDAOImpl implements UserDAO {
// 根据用户名和密码查询用户
    @Override
    public User loginUsername(String username, String passwrod) {
        User user = null;
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            conn = DbUtil.getConnection();
            String sql = "select id, username, password from users where username = ? and password = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, passwrod);
            rs = pst.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(rs, pst, conn);
        }

        return user;
    }
// 根据用户名查询用户信息
    @Override
    public User getUserByUsername(String username) {
        User user = null;
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            conn = DbUtil.getConnection();
            String sql = "select * from users where username = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            rs = pst.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setLevel(rs.getString("level"));
                user.setImage(rs.getString("image"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                user.setTime(rs.getDate("register_time"));
                user.setStatus(rs.getInt("status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(rs, pst, conn);
        }

        return user;
    }

    // 插入新用户
    @Override
    public boolean insertUser(User user) {
        Connection conn = null;
        PreparedStatement pst = null;

        try {
            conn = DbUtil.getConnection();
            String sql = "INSERT INTO users (username, password, email, phone) VALUES (?, ?, ?, ?)";
            pst = conn.prepareStatement(sql);
            pst.setString(1, user.getUsername());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getEmail());
            pst.setString(4, user.getPhone());

            int rowsAffected = pst.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DbUtil.close(null, pst, conn);
        }
    }
}
